<?php
/*
 * @Descripttion: 
 * @Author: Twoknives
 * @Date: 2023-11-13 21:22:43
 * @LastEditTime: 2024-10-11 21:25:23
 * @FilePath: \xa-zhongcai-server\app\api\model\User.php
 */

namespace app\api\model;

use think\Model;

use app\api\controller\Auth;
use app\api\model\Token;
use think\model\concern\SoftDelete;

/**
 * 用户模型
 */
class ApiUser extends Model
{
  use SoftDelete;

  // protected $deleteTime = 'delete_time';


  protected $name = 'sys_user';

  // 开启自动写入时间戳字段
  protected $auto_timestamp = 'false';

  /**
   * 编辑信息
   */
  public function edit($data)
  {
    $uid = $data['user_id'];
    unset($data['user_id']);
    $data['update_by'] = date('Y-m-d H:i:s');
    $data = array_filter($data);
    $user = $this->where('user_id',$uid)->find();
    $rst = $user->allowField(['user_name', 'avatar','status','update_by'])->save($data);
    return $rst;
  }

  /**
   * 用户列表
   */
  public function getPcList($pagination)
  {
    $where = [];
    $query = empty($pagination['query']) ? [] : $pagination['query'];
    if (!is_array($query)) {
      $query = json_decode(htmlspecialchars_decode($query), true);
    }
    if ($query && !empty($query['user_name'])) {
      $where[] = ['user_name', 'like', '%' . $query['user_name'] . '%'];
    }


    $list = $this->where($where)->where('del_flag', 0)->order('create_by', 'desc')
    ->paginate([
      'list_rows' => !empty($pagination['pageSize']) ? $pagination['pageSize'] : 10,
      'page' =>  !empty($pagination['currentPage']) ? $pagination['currentPage'] : 1,
    ]);
    return $list;
  }

  /**
   * 详情
   */
  public function getDetail($where = [])
  {
    $rst = $this->where('del_flag', 0)->where('status', 0)->where($where)->find();
    return $rst;
  }

  /**
   * @name: 用户登录
   * @param {*} $name
   * @param {*} $pwd
   * @return {*}
   */
  // public function doLogin($uid)
  // {
  //   $userInfo = $this->where('user_id', $uid)->where('status', 0)->where('del_flag', 0)->find();
  //   return $userInfo;
  // }

  /**
   * 第三方登录
   */
  public function thirdLogin($openid, $from_type, $device_type, $unionid = '')
  {
    $userInfo = $this->where('openid', $openid)->where('from_type', $from_type)->where('del_flag', 0)->find();
    if ($userInfo) {
      $token = Auth::makeToken();
      $userToken = new Token();
      $uid = $userInfo['user_id'];
      $userToken->setToken($token, $uid, $device_type);
      return  ['status' => 'success', 'msg' => "登录成功", 'data' => ['token' => $token, 'method' => $device_type]];
      // $this->success('登录成功', ['token' => $token, 'method' => $this->method]);
    } else {
      $regRst = ApiUser::create(['from_type' => $from_type, 'openid' => $openid, 'create_by' => date('Y-m-d H:i:s'),'unionid'=>$unionid]);
      $uid = $regRst->id;
      if ($regRst) {
        $token = Auth::makeToken();
        $userToken = new Token();
        $userToken->setToken($token, $uid, $device_type);
        return  ['status' => 'success', 'msg' => "登录成功", 'data' => ['token' => $token, 'method' => $device_type]];
      } else {
        return  ['status' => 'error', 'msg' => "登录失败"];
      }
    }
  }

  public function saveInfo($info, $uid)
  {
    if (!empty($info['id'])) {
      $info['user_id'] = $info['id'];
      unset($info['id']);
    }
    $currentUsr = $this->where('user_id', $uid)->find();
    $info['update_by'] = $currentUsr['user_name'];
    $data = $this->save($info);
    return $data;
  }

  public function updateInfo($info, $uid)
  {
    if (empty($info['user_id'])) {
      $this->error('未找指定用户');
    }
    $currentUsr = $this->where('user_id', $uid)->find();
    $info['update_by'] = $currentUsr['user_name'];

    if (!empty($info['id'])) {
      $info['user_id'] = $info['id'];
      unset($info['id']);
    }

    $info['update_time'] = date('Y-m-d H:i:s');
    $data = $this->where('user_id', $info['user_id'])->update($info);
    return $data;
  }

  public function del($info)
  {
    $upDate = [
      'del_flag' => '2'
    ];
    $data = $this->where('user_id', $info['user_id'])->update($upDate);
    return $data;
  }
}
